<?php
class Customer_Model_Customer
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Customer_Model_DbTable_Customer();
	}
	
	
	/**
	*Create A Record  
	*@param rowSet $row Department's data 
	*@return boolean
	*/
	public  function add($row){	 
 		return $this->_table->insert($row); 
	}
	
	/**
	*Update One Row  
	*@param rowSet $row Department's data 
	*@param int $ud_id 
	*@return boolean
	*/
	public  function update($row,$id){
		$where = $this->_table->getAdapter()->quoteInto('customer_id = ?', $id);
		return $this->_table->update($row, $where);	
	}
		
	/**
	*No Delete One Row
	*@param int $ud_id 
	*@return boolean
	*/
	 public function delete($id){	
		$where = $this->_table->getAdapter()->quoteInto('customer_id = ?', $id); 
		return $this->_table->delete($where);		
	}
	
	/**
	*Get Rowset By user 
	*@param string $ud_id login id
	*@return Rowset
	*/
	public function getByCustomerId($id){
		$select = $this->_table->select()->where('customer_id = ?', $id);	 
		return $this->_table->fetchRow($select);
	}
	
	public function getByCustomerName($CustomerName){
		$select = $this->_table->select()->where('customer_name = ?', $CustomerName);	 
		return $this->_table->fetchRow($select);
	}
	
	public function getByCond($cond,$type,$order='customer_id',$count=20, $offset=0){
		$table = $this->_table->info('name');
		$select = $this->_table->select()->from($table, $type);
		if(isset($cond['customer_id']) && $cond['customer_id']){
			$select->where('customer_id = ?',$cond['customer_id']);
		}
		if(isset($cond['customer_status']) && $cond['customer_status'] !==''){
			$select->where('customer_status = ?',$cond['customer_status']);
		}
		if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }
        if ($count==0){        	
        	return $this->_table->fetchAll($select)->toArray(); 
        }
		$select->order($order);
		$select->limit($count,$offset);
		$rowset = $this->_table->fetchAll($select);
		return $rowset->toArray();	
	}

	public function getByGwemCustomerId($customer_id){
		$select = $this->_table->select()->where('customer_id = ?', $customer_id)->where('date_add(customer_date_reg,interval 1 month)>now()');
		return $this->_table->fetchRow($select);
	}
}